package logic

import (
	"context"
	"encoding/json"
	"hippo/cache"
	"hippo/models"
	"time"

	"hippo/hipporpc/hipporpc"
	"hippo/hipporpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLogic {
	return &ShopListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表
func (l *ShopListLogic) ShopList(in *hipporpc.ShopListRequest) (*hipporpc.ShopListResponse, error) {
	// todo: add your logic here and delete this line

	var shop models.Shop
	var list []*hipporpc.ShopList

	result, _ := cache.Rdb.Get(l.ctx, "shopList").Result()
	if result == "" {
		var err error
		list, err = shop.ShopList(models.DB)
		if err != nil {
			return nil, err
		}
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(l.ctx, "shopList", marshal, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &hipporpc.ShopListResponse{
		List: list,
	}, nil
}
